package com.cinc.ebc.mm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.mm.entity.MmMaterialStock;
import com.cinc.ebc.mm.model.MmRptMaterialStockWarehouseStatModel;
import com.cinc.ebc.mm.model.MmRptMaterialStockWarehouseStatTotalModel;
import com.cinc.ebc.mm.model.MmRptPurOrderMaterialStatTotalModel;
import com.cinc.ebc.mm.service.IMmMaterialStockService;
import com.cinc.ebc.mm.model.MmMaterialStockModel;
import com.cinc.ebc.mm.vo.MmRptMaterialStockStatPage;
import com.cinc.ebc.mm.vo.MmRptPurOrderStatPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 库存量
 * @Author: jeecg-boot
 * @Date:   2022-04-10
 * @Version: V1.0
 */
@Api(tags="库存量")
@RestController
@RequestMapping("/mm/materialStock")
@Slf4j
public class MmMaterialStockController extends JeecgController<MmMaterialStock, IMmMaterialStockService> {
	@Autowired
	private IMmMaterialStockService mmMaterialStockService;

	/**
	 * 分页列表查询
	 *
	 * @param mmMaterialStock
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "库存量-分页列表查询")
	@ApiOperation(value="库存量-分页列表查询", notes="库存量-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(MmMaterialStock mmMaterialStock,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MmMaterialStock> queryWrapper = QueryGenerator.initQueryWrapper(mmMaterialStock, req.getParameterMap());
		queryWrapper.gt("stock_qty",BigDecimal.ZERO);
		String projectName = req.getParameter("projectName");
		if(oConvertUtils.isNotEmpty(projectName)){
			queryWrapper.inSql("project_id","select id from quote_project where project_name like '%" + projectName + "%'");
		}
		Page<MmMaterialStock> page = new Page<MmMaterialStock>(pageNo, pageSize);
		IPage<MmMaterialStock> pageList = mmMaterialStockService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param mmMaterialStockModel
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "可用库存量-分页列表查询")
	 @ApiOperation(value="可用库存量-分页列表查询", notes="可用库存量-分页列表查询")
	 @GetMapping(value = "/availableList")
	 public Result<?> queryPageAvailableList(MmMaterialStockModel mmMaterialStockModel,
											 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											 HttpServletRequest req) {
		 MmMaterialStock mmMaterialStock = new MmMaterialStock();
		 BeanUtils.copyProperties(mmMaterialStockModel, mmMaterialStock);
		 QueryWrapper<MmMaterialStock> queryWrapper = QueryGenerator.initQueryWrapper(mmMaterialStock, req.getParameterMap());
		 queryWrapper.gt("stock_qty_available", BigDecimal.ZERO);
		 if(oConvertUtils.isNotEmpty(mmMaterialStockModel.getMaterialIds())){
			 List<String> materialIdList = Arrays.asList(mmMaterialStockModel.getMaterialIds().split(","));
			 queryWrapper.in("material_id", materialIdList);
		 }
		 Page<MmMaterialStock> page = new Page<MmMaterialStock>(pageNo, pageSize);
		 IPage<MmMaterialStock> pageList = mmMaterialStockService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 列表查询
	  *
	  * @param mmMaterialStock
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "库存量-列表查询")
	 @ApiOperation(value="库存量-分页列表查询", notes="库存量-分页列表查询")
	 @GetMapping(value = "/allList")
	 public Result<?> queryList(MmMaterialStock mmMaterialStock, HttpServletRequest req) {
		 QueryWrapper<MmMaterialStock> queryWrapper = QueryGenerator.initQueryWrapper(mmMaterialStock, req.getParameterMap());
		 List<MmMaterialStock> allList = mmMaterialStockService.list(queryWrapper);
		 return Result.OK(allList);
	 }

	/**
	 *   添加
	 *
	 * @param mmMaterialStock
	 * @return
	 */
	@AutoLog(value = "库存量-添加")
	@ApiOperation(value="库存量-添加", notes="库存量-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody MmMaterialStock mmMaterialStock) throws Exception {
		mmMaterialStockService.saveMaterialStock(mmMaterialStock);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param mmMaterialStock
	 * @return
	 */
	@AutoLog(value = "库存量-编辑")
	@ApiOperation(value="库存量-编辑", notes="库存量-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody MmMaterialStock mmMaterialStock) throws Exception {
		mmMaterialStockService.updateById(mmMaterialStock);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存量-通过id删除")
	@ApiOperation(value="库存量-通过id删除", notes="库存量-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		mmMaterialStockService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库存量-批量删除")
	@ApiOperation(value="库存量-批量删除", notes="库存量-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mmMaterialStockService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存量-通过id查询")
	@ApiOperation(value="库存量-通过id查询", notes="库存量-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		MmMaterialStock mmMaterialStock = mmMaterialStockService.getById(id);
		if(mmMaterialStock==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mmMaterialStock);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mmMaterialStock
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MmMaterialStock mmMaterialStock) {
        return super.exportXls(request, mmMaterialStock, MmMaterialStock.class, "库存量");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MmMaterialStock.class);
    }

	 /**
	  * 库房库存量统计
	  *
	  * @param mmRptMaterialStockStatPage
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "库房库存量统计")
	 @ApiOperation(value="库房库存量统计", notes="库房库存量统计")
	 @GetMapping(value = "/rptWarehouseStatList")
	 public Result<?> queryRptWarehouseStatList(MmRptMaterialStockStatPage mmRptMaterialStockStatPage,
												@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												HttpServletRequest req) {
		 Page<MmRptMaterialStockWarehouseStatModel> page = new Page<>(pageNo, pageSize);
		 IPage<MmRptMaterialStockWarehouseStatModel> pageList = mmMaterialStockService.queryRptWarehouseStatList(page, mmRptMaterialStockStatPage);
		 return Result.OK(pageList);
	 }

	 /**
	  * 库存统计查询-库房-列合计
	  *
	  * @param mmRptMaterialStockStatPage
	  * @return
	  */
	 @AutoLog(value = "库存统计查询-库房-列合计")
	 @ApiOperation(value="库存统计查询-库房-列合计", notes="库存统计查询-库房-列合计")
	 @GetMapping(value = "/rptWarehouseStatTotal")
	 public Result<?> queryRptWarehouseStatTotal(MmRptMaterialStockStatPage mmRptMaterialStockStatPage) {

		 MmRptMaterialStockWarehouseStatTotalModel data = mmMaterialStockService.queryRptWarehouseStatTotal(mmRptMaterialStockStatPage);
		 return Result.OK(data);
	 }
}
